package controllers

import config.{Global, MyAwait, MyFile, MyRow}
import dao._
import play.api.libs.json.Json
import play.api.mvc.{AbstractController, ControllerComponents}

import javax.inject.Inject
import scala.concurrent.ExecutionContext

class PhenotypeController @Inject()(phenotypeDao: PhenotypeDao,
                                    cc: ControllerComponents)
                                   (implicit exec: ExecutionContext) extends AbstractController(cc) with MyAwait with MyRow with MyFile {

  def phenotypePage = Action { implicit request =>
    Ok(views.html.phenotype.phenotypePage())
  }

  def getAllPhenotypeInfo = Action.async { implicit request =>
    phenotypeDao.getAll.map { x =>
      Ok(Json.toJson(x.map(_.getJsonByT())))
    }
  }

  def pheInfoPage(id: Int) = Action.async { implicit request =>
    phenotypeDao.getById(id).map { x =>
      val files = s"${Global.path}/config/img".listFile.filter(_.getName.startsWith(x.img + "_"))
      val name = if (files.isEmpty) {
        Array("null")
      } else {
        files.map(_.getName)
      }
      Ok(views.html.phenotype.pheInfoPage(x,name.toSeq))
    }
  }

}
